package com.HelloWidget;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class NationalGeographicPhotoOfTheDay extends PhotoContentDescriptor {
	private Element _root;

	public  void init () throws IOException {

			_doc = Jsoup.connect("http://photography.nationalgeographic.com/photography/?source=NavPhoHome").get();
			_root = _doc.getElementById("webslice1");
			//System.out.println("NG _root1:"+_root.html());
			Elements elems = _root.getElementsByClass("first");
			_root = elems.first();
			//System.out.println("NG _root2:"+_root.html());
			_initialized = true;
		
	}

	@Override
	public CharSequence getHeadline() {
		// TODO Auto-generated method stub
		return "Photo of the Day";
	}

	@Override
	public CharSequence getDescription() {
		//System.out.println("NG _root2:"+_root.html());

		Element img = _root.getElementsByTag("img").first();
		String alt = img.attr("alt");
		// System.out.println("img a 1st:"+img.toString());
		int start = alt.indexOf(":");
		//int end = alt.indexOf("\"",start+1);
		//System.out.println("start:"+start);

		return alt.substring(start+2); //after ':' there is a space

	}

	@Override
	public String getBitmapUrl() {
		Element img = _root.getElementsByTag("img").first();
		// System.out.println("img a 1st:"+img.toString());
		int start = img.toString().indexOf("\"");
		int end = img.toString().indexOf("\"",start+1);
		//System.out.println("start:"+start+" end:"+end);

		return img.toString().substring(start+1, end);
	}

	@Override
	public String getUrl() {
		Element img = _root.getElementsByTag("a").first();
		// System.out.println("img a 1st:"+img.toString());
		int start = img.toString().indexOf("\"");
		int end = img.toString().indexOf("\"",start+1);
		//System.out.println("start:"+start+" end:"+end);

		return "http://photography.nationalgeographic.com"+img.toString().substring(start+1, end);

	}

}
